//
//  CommonCellViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 15-1-17.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "CommonCellViewController.h"
#import "SportsDetailViewController.h"
#import "TransforController.h"
#import "PopoverView.h"
#import "BMapKit.h"

@interface CommonCellViewController ()<UITableViewDelegate, UITableViewDataSource, PullTableViewDelegate, BMKLocationServiceDelegate> {
    BMKLocationService* _locService;
}

@property (nonatomic,strong) UIView* headerView;
@property (nonatomic,strong) UIView* headerPopupView;

@property (nonatomic,assign) int currentIndex;
@property (nonatomic,strong) NSMutableArray* storesList;
@property (nonatomic,assign) int typeId;
@property (nonatomic,assign) double latitude;
@property (nonatomic,assign) double longitude;

@property (nonatomic, strong)NSArray *array_districtId;
@property (nonatomic, strong)NSArray *array_districtName;
@property (nonatomic, strong)NSArray *array_extraTypeId;
@property (nonatomic, strong)NSArray *array_extraTypeName;

@property (nonatomic, assign)int chosenTag;
@property (nonatomic, assign)int chosenIndex;
@property (nonatomic, assign)BOOL isExtraChoosable;
@property (nonatomic, assign)BOOL isDistrictChoosable;
@property (nonatomic, strong) NSString *string_distance;
@property (nonatomic, strong) NSString *string_district;
@property (nonatomic, strong) NSString *string_extra;

@property (nonatomic, strong) PullTableView *tableView;
@end

@implementation CommonCellViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self openLocation];
    self.array_districtName = [NSArray arrayWithObjects:@"正在加载", nil];
    self.array_extraTypeName = [NSArray arrayWithObjects:@"正在加载", nil];
    
    // Do any additional setup after loading the view.
    self.currentIndex = -1;
    
//    [self setEnableRefresh:NO];
    
    [self getDistrictFromAPI];
    [self getExtraTypeFromAPI];
    self.string_distance = @"100";
    self.string_district = nil;
    self.string_extra = nil;
    [self.view setFrame:[UIScreen mainScreen].bounds];
    
    self.tableView = [[PullTableView alloc]initWithFrame:CGRectMake(0, 65, 320, self.view.bounds.size.height) style:UITableViewStylePlain];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
        [self setEnableRefresh:YES];
    [self setEnableLoadMore:YES];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.pullDelegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.frame = CGRectMake(0, self.tableView.tableHeaderView.frame.size.height + 30, self.view.bounds.size.width, self.view.bounds.size.height - self.headerView.bounds.size.height);
    self.view.frame = CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, self.tableView.bounds.size.width, 0.f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    [self getStoreList];
    
    UIView *view_temp = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0];
    [self.view addSubview:view_temp];
    
    self.isExtraChoosable = false;
    self.isDistrictChoosable = false;
    [self resetChosen];
    if(self.isDisplayTopMenu){
        [self topSelectMenuReloadData];
        self.tableView.frame = CGRectMake(0, self.tableView.tableHeaderView.frame.size.height + HEIGHT_STATUS_BAR + HEIGHT_NAVIGATION_BAR + 35, self.view.bounds.size.width, self.view.bounds.size.height - self.headerView.bounds.size.height - (HEIGHT_STATUS_BAR + HEIGHT_NAVIGATION_BAR) - 34);
    }else{
        self.tableView.frame = CGRectMake(0, self.tableView.tableHeaderView.frame.size.height + HEIGHT_STATUS_BAR + HEIGHT_NAVIGATION_BAR, self.view.bounds.size.width, self.view.bounds.size.height - self.headerView.bounds.size.height - (HEIGHT_STATUS_BAR + HEIGHT_NAVIGATION_BAR));
    }
    
    UILabel *nothing = [[UILabel alloc] initWithFrame:CGRectMake(55, 200, 200, 20)];
    nothing.textAlignment = UITextAlignmentCenter;
    nothing.textColor = [Common colorWithRGB:0xb2b2b2];
    nothing.text = @"加载中...";
    nothing.tag = 40000;
    [self.tableView addSubview:nothing];
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)topSelectMenuReloadData{
    UIView *view_topSelectMenu = [[UIView alloc]initWithFrame:CGRectMake(0, (HEIGHT_STATUS_BAR + HEIGHT_NAVIGATION_BAR), 320, 35)];
    view_topSelectMenu.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1];
    UIButton *btn_all=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn_all setFrame:CGRectMake(0, 0, 106 , 35)];
//    [btn_all setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:225/255.0]];
    [btn_all setTag:50001];
    [btn_all setTitle:@"附近" forState:UIControlStateNormal];
    [btn_all setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn_all setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [btn_all.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn_all addTarget:self action:@selector(topSelectMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
    [view_topSelectMenu addSubview:btn_all];
    
    UIButton *btn_second=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn_second setFrame:CGRectMake(106, 0, 106 , 35)];
    //    [btn_all setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:225/255.0]];
    [btn_second setTag:50002];
    [btn_second setTitle:@"筛选" forState:UIControlStateNormal];
    [btn_second setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn_second setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [btn_second.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn_second addTarget:self action:@selector(topSelectMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
    [view_topSelectMenu addSubview:btn_second];
    
    UIButton *btn_third=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn_third setFrame:CGRectMake(212, 0, 106 , 35)];
    //    [btn_all setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:225/255.0]];
    [btn_third setTag:50003];
    [btn_third setTitle:@"区域" forState:UIControlStateNormal];
    [btn_third setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn_third setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [btn_third.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn_third addTarget:self action:@selector(topSelectMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
    [view_topSelectMenu addSubview:btn_third];
    
    [self.view addSubview:view_topSelectMenu];
}

- (void)topSelectMenuClicked:(UIButton *)sender{
    UIButton *btn_temp = sender;
    CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width / 2, 85);
    PopoverView *pop = nil;
    switch (btn_temp.tag) {
        case 50001:
//            [self resetChosen];
//            [self pullTableViewDidTriggerRefresh:self.tableView];
            pop = [[PopoverView alloc]initWithPoint:point titles:[NSArray arrayWithObjects:@"500米", @"1公里",@"3公里",@"5公里", nil] images:nil];
            [pop setTag:40000];
            break;
        case 50002:
            pop = [[PopoverView alloc] initWithPoint:point titles:self.array_extraTypeName images:nil];
            [pop setTag:40001];
            break;
        case 50003:
            pop = [[PopoverView alloc] initWithPoint:point titles:self.array_districtName images:nil];
            [pop setTag:40002];
            break;
        default:
            pop = nil;
            break;
    }
    
//    NSArray *titles = @[@"item1", @"选项2", @"选项3"];
//    NSArray *images = @[@"28b.png", @"28b.png", @"28b.png"];
//    PopoverView *pop = [[PopoverView alloc] initWithPoint:point titles:titles images:images];
    if(pop.tag == 40001){
        self.chosenTag = 40001;
        pop.selectRowAtIndex = ^(NSInteger index){
            NSLog(@"%s || select index:%d and content:%@", __FUNCTION__, index, [self.array_extraTypeName objectAtIndex:index]);
            self.chosenIndex = index;
            if(self.isExtraChoosable){
                [self pullTableViewDidTriggerRefresh:self.tableView];
            }
        };
    }else if(pop.tag == 40002){
        self.chosenTag = 40002;
        pop.selectRowAtIndex = ^(NSInteger index){
            NSLog(@"%s || select index:%d and content:%@", __FUNCTION__, index, [self.array_districtName objectAtIndex:index]);
            self.chosenIndex = index;
            if(self.isDistrictChoosable){
                [self pullTableViewDidTriggerRefresh:self.tableView];
            }
        };
    }else{
//        [self resetChosen];
        self.chosenTag = 40000;
        pop.selectRowAtIndex = ^(NSInteger index){
            NSLog(@"%s || select index: %d", __FUNCTION__, index);
            self.chosenIndex = index;
            [self pullTableViewDidTriggerRefresh:self.tableView];
            switch (index) {
                case 0:
                    self.string_distance = @"500";
                    break;
                case 1:
                    self.string_distance = @"1000";
                    break;
                case 2:
                    self.string_distance = @"3000";
                    break;
                case 3:
                    self.string_distance = @"5000";
                    break;
                default:
                    self.string_distance = @"100";
                    break;
            }
        };
    }
    [pop show];
}

- (void)resetChosen{
    self.chosenTag = 0;
    self.chosenIndex = 0;
}

- (void)getExtraTypeFromAPI{
    [self getTypeId];
    [[ProtocolManager shareInstance] getFLWTEBFliterExtraTypeWithTypeId:self.typeId andTarget:self andSelector:@selector(getExtraTypeFromAPICallBack:)];
}

- (void)getExtraTypeFromAPICallBack:(NSDictionary *)dic {
    if(dic == nil){
        [self resetPullTableViewState];
        return ;
    }
    NSLog(@"%s || dic:%@", __FUNCTION__, dic);
    NSArray *array_temp = [NSArray arrayWithArray:[Common valueForKey:@"types" dic:dic]];
    NSMutableArray *array_temp2 = [[NSMutableArray alloc]init];
    NSMutableArray *array_temp3 = [[NSMutableArray alloc]init];
    if([array_temp count] < 1 || array_temp == nil){
        [array_temp2 addObject:@"-1"];
        [array_temp3 addObject:@"没有筛选条件"];
        self.isExtraChoosable = false;
    }else{
        for(NSDictionary *dic_temp in array_temp){
            [array_temp2 addObject:[Common valueForKey:@"id" dic:dic_temp]];
            [array_temp3 addObject:[Common valueForKey:@"name" dic:dic_temp]];
        }
        self.isExtraChoosable = true;
    }
    self.array_extraTypeId = [NSArray arrayWithArray:array_temp2];
    self.array_extraTypeName = [NSArray arrayWithArray:array_temp3];
    [self resetPullTableViewState];
}

- (void)getDistrictFromAPI{
    [[ProtocolManager shareInstance] getFLWTEBFliterDistrictWithTarget:self andSelector:@selector(getDistrictFromAPICallBack:)];
}//使用API

- (void)getDistrictFromAPICallBack:(NSDictionary *)dic{
    if(dic == nil){
        return;
    }
    NSLog(@"%s || dic:%@", __FUNCTION__, dic);
    NSArray *array_temp = [NSArray arrayWithArray:[Common valueForKey:@"areas" dic:dic]];
    NSMutableArray *array_temp2 = [[NSMutableArray alloc]init];
    NSMutableArray *array_temp3 = [[NSMutableArray alloc]init];
    if([array_temp count] < 1 || array_temp == nil){
        [array_temp2 addObject:@"-1"];
        [array_temp3 addObject:@"没有筛选条件"];
        self.isExtraChoosable = false;
    }else{
        for(NSDictionary *dic_temp in array_temp){
            [array_temp2 addObject:[Common valueForKey:@"id" dic:dic_temp]];
            [array_temp3 addObject:[Common valueForKey:@"name" dic:dic_temp]];
        }
        self.isDistrictChoosable = true;
    }
    self.array_districtId = [NSArray arrayWithArray:array_temp2];
    self.array_districtName = [NSArray arrayWithArray:array_temp3];
}

#pragma mark - Protocol request
- (void)getStoreList
{
    [self getTypeId];
    if(self.chosenTag == 40000){
        NSString *string_temp = nil;
        if (self.chosenIndex == 0) {
            string_temp = @"500";
        }else if(self.chosenIndex == 1){
            string_temp = @"1000";
        }else if(self.chosenIndex == 2){
            string_temp = @"3000";
        }else{
            string_temp = @"5000";
        }
        [[ProtocolManager shareInstance] getFLWTEBList:10 beginNo:_currentIndex + 1 typeId:_typeId API:_API_REQ[0] REQ:_API_REQ[1] andDistance:string_temp tag:self.string_extra areaName:self.string_district target:self selector:@selector(getStoreListCallback:)];
    }else if(self.chosenTag == 40001){
        self.string_extra = [self.array_extraTypeName objectAtIndex:self.chosenIndex];
        [[ProtocolManager shareInstance] getFLWTEBList:10 beginNo:_currentIndex + 1 typeId:_typeId API:_API_REQ[0] REQ:_API_REQ[1] andDistance:self.string_distance tag:self.string_extra areaName:nil target:self selector:@selector(getStoreListCallback:)];
    }else if(self.chosenTag == 40002){
        self.string_district = [self.array_districtName objectAtIndex:self.chosenIndex];
        [[ProtocolManager shareInstance] getFLWTEBList:10 beginNo:_currentIndex + 1 typeId:_typeId API:_API_REQ[0] REQ:_API_REQ[1] andDistance:self.string_distance tag:self.string_extra areaName:self.string_district target:self selector:@selector(getStoreListCallback:)];
    } else {
        [[ProtocolManager shareInstance] getFLWTEBList:10 beginNo:_currentIndex + 1 typeId:_typeId API:_API_REQ[0] REQ:_API_REQ[1] andDistance:@"300000000" tag:self.string_extra areaName:self.string_district target:self selector:@selector(getStoreListCallback:)];
    }
    NSLog(@"%s || current chosen status:%@, %@, %@", __FUNCTION__, self.string_distance, self.string_extra, self.string_district);
}

#pragma mark - Protocol call back

- (void)getStoreListCallback:(NSDictionary*)dic
{
//    NSLog(@"%s || getListCallback %@", __FUNCTION__, dic);
    NSLog(@"%d",_currentIndex + 1);
    UILabel *nothing = (UILabel*)[self.tableView viewWithTag:40000];
    nothing.text = @"暂无结果...";
    nothing.hidden = NO;
    if (dic == nil) {
        [self resetPullTableViewState];
        return;
    }
    if (self.currentIndex == -1) {
        self.storesList = [NSMutableArray array];
    }
    NSArray* storesList = [Common valueForKey:P_V2_STORES dic:dic];
    if ([storesList isKindOfClass:[NSArray class]]) {
        [self.storesList addObjectsFromArray:storesList];
        self.currentIndex += storesList.count;
    }
    if (storesList.count < STORE_LIST_RETURN_COUNT) {
        [self setEnableLoadMore:NO];
    }
    else {
        if ([self.is_load_more isEqualToString:@"NO"]) {
            [self setEnableLoadMore:NO];
        } else {
            [self setEnableLoadMore:YES];
        }
    }
    [self resetChosen];
    [self resetPullTableViewState];
    [self.tableView reloadData];

}

#pragma mark - Cell
- (void)cellSetup:(UITableViewCell*)cell index:(NSInteger)index;
{
    if (self.storesList.count <= 0) {
        return;
    }
    UILabel *nothing = (UILabel*)[self.tableView viewWithTag:40000];
    nothing.hidden = YES;
    NSDictionary *cellDic = _storesList[index];
    NSString *imgUrl = [Common valueForKey:P_LOGO_PATH dic:cellDic];
    NSString *title = [Common valueForKey:P_NAME dic:cellDic];
    NSString *districtName = [Common valueForKey:P_V2_DISTRICT_NAME dic:cellDic];
    NSString *type_name = [Common valueForKey:P_LABEL dic:cellDic];
    NSString *good = [Common valueForKey:P_V2_GOOD dic:cellDic];
    NSString *comment_cnt = [Common valueForKey:P_V2_COMMENT_CNT dic:cellDic];
    NSString *base_price = [Common valueForKey:P_BASE_PRICE dic:cellDic];
    NSString *gps = [Common valueForKey:P_V2_GPS dic:cellDic];
    NSString *webSite =[NSString stringWithFormat:@"%@",[Common valueForKey:P_V2_HAS_MICRO_SITE dic:cellDic]];
    UIImageView *websiteView = (UIImageView*)[cell viewWithTag:1007];
    if ([webSite isEqualToString:@"0"]) {
        websiteView.hidden = YES;
    }
    double distance = 0;
    UIImageView *image = (UIImageView*)[cell viewWithTag:1001];
    [image setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    UILabel *titleLab = (UILabel*)[cell viewWithTag:1002];
    //[titleLab setNumberOfLines:2];
    titleLab.text = title;
    //[titleLab sizeToFit];
    //CGRect frame = titleLab.frame;
    //frame.size.width = 216;
    //titleLab.frame = frame;
    [self tagSetup:cellDic titleLabHeight:-1 cell:cell];
    UILabel *price = (UILabel*)[cell viewWithTag:1005];
    if ([base_price isEqualToString:@""] || [base_price isEqualToString:@"免费"] || base_price == nil){
        [price removeFromSuperview];
    } else {
        NSArray *priceArray = [base_price componentsSeparatedByString:@"元"];
        price.text = [NSString stringWithFormat:@"%@%@",priceArray[0],@"元起"];
    }
    UILabel *districtNameLab = (UILabel*)[cell viewWithTag:2002];
    if (![gps isEqualToString:@""]) {
        distance = [self getDistance:gps] / 1000.0;
    }
    districtNameLab.text = [NSString stringWithFormat:@"    %.1fkm%@", distance, districtName];
    if (_latitude <= 0 && _longitude <= 0) {
        districtNameLab.text = [NSString stringWithFormat:@"    %@", districtName];
    }
    UILabel* thirdLab = (UILabel*)[cell viewWithTag:2007];
    thirdLab.text = type_name;
    UILabel* goodLab = (UILabel*)[cell viewWithTag:2004];
    goodLab.text = good;
    UILabel* CommontLab = (UILabel*)[cell viewWithTag:2006];
    CommontLab.text = comment_cnt;
}

- (double)getDistance:(NSString*)gps {
    NSArray *gpsArray = [gps componentsSeparatedByString:@","];
    double targetLat = [gpsArray[1] doubleValue];
    double targetLon = [gpsArray[0] doubleValue];
    self.latitude = [ProtocolManager shareInstance].latitude;
    self.longitude = [ProtocolManager shareInstance].longitude;
    CLLocationCoordinate2D c1;
    c1 = CLLocationCoordinate2DMake(targetLat, targetLon);
    CLLocationCoordinate2D c2;
    c2 = CLLocationCoordinate2DMake(_latitude, _longitude);
    BMKMapPoint point1 = BMKMapPointForCoordinate(c1);
    BMKMapPoint point2 = BMKMapPointForCoordinate(c2);
    CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2);
    return distance;
}

//标签描绘
- (void)tagSetup:(NSDictionary*)cellDic titleLabHeight:(CGFloat)height cell:(UITableViewCell*)cell
{
    NSArray *tagsList = [Common valueForKey:P_TAGS dic:cellDic];
    NSString *open_time = [Common valueForKey:P_OPEN_TIME dic:cellDic];
    UIView* tagListView = tagListView = [cell viewWithTag:1003];
    if(tagsList.count <= 0){
        UILabel *openTimeLab = (UILabel*)[cell viewWithTag:2008];
        openTimeLab.text = open_time;
    } else {
    CGFloat tagX = 0.0;
    int count = [tagsList count] > 3 ? 3 : [tagsList count];
    for (int i = 0; i < count; i++) {
        NSDictionary *tagDic = tagsList[i];
        NSString *cover_path = [Common valueForKey:P_COVER_PATH dic:tagDic];
        NSArray *rgbArray = [[Common valueForKey:P_V2_RGB dic:tagDic] componentsSeparatedByString:@"#"];
        if ([cover_path isEqualToString:@""]) {
            NSString *rgb = [NSString stringWithFormat:@"%@%@", @"0x",@"18aaf6"];
            if (rgbArray.count > 1) {
                rgb = [NSString stringWithFormat:@"%@%@", @"0x",rgbArray[1]];
            }
            NSString *name = [Common valueForKey:P_NAME dic:tagDic];
            UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(tagX, 0, 45, 16)];
            tagView.layer.cornerRadius = 3.0;
            unsigned int color = strtoul([rgb UTF8String],0,0);//nsstring 转 rgb
            tagView.backgroundColor = [Common colorWithRGB:color];
            UILabel *tagNameLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tagView.frame.size.width, tagView.frame.size.height)];
            tagNameLab.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:10.0];
            tagNameLab.textAlignment = UITextAlignmentCenter;
            tagNameLab.textColor = [UIColor whiteColor];
            tagNameLab.text = name;
            [tagNameLab sizeToFit];
            CGRect frame = tagNameLab.frame;
            frame.size.width = MAX(tagNameLab.frame.size.width + 5, 45);
            frame.size.height = 16;
            frame.origin = tagView.frame.origin;
            tagView.frame = frame;
            frame.origin = tagNameLab.frame.origin;
            tagNameLab.frame = frame;
            [tagView addSubview:tagNameLab];
            [tagListView addSubview:tagView];
            tagX += tagView.frame.size.width + 5;
        } else {
            UIImageView *tagsImg = [[UIImageView alloc]initWithFrame:CGRectMake(tagX, 0, 12, 12)];
            [tagsImg setImageWithURL:[Common getFullDownloadPathByRelativePath:cover_path]];
            [tagListView addSubview:tagsImg];
            tagX += 20;
        }
    }
    }
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.storesList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommonCell"];
    // Configure the cell...
    if(cell == nil)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CommonCell"  owner:self options:nil][0];
    }
    [self cellSetup:cell index:indexPath.row];
    return cell;
}

#pragma mark - Table view delegate
//Bable-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row >= self.storesList.count) {
        return;
    }
    NSDictionary* dic = self.storesList[indexPath.row];
    int storeId = [[Common valueForKey:P_ID dic:dic] intValue];
    SportsDetailViewController *testView = [[SportsDetailViewController alloc]init];
    testView.target_id = storeId;
    [self.navigationController pushViewController:testView animated:YES];
    

    
        
    
    

}


#pragma mark - private
- (void)getTypeId
{
    if ([_type  isEqualToString: @"FOOD"]) {
        self.typeId = 9;
    } else if ([_type  isEqualToString: @"LIVE"]) {
        self.typeId = 5;
    } else if ([_type  isEqualToString: @"WALK"]) {
        self.typeId = 4;
    } else if ([_type  isEqualToString: @"TRAVEL"]) {
        self.typeId = 6;
    } else if ([_type  isEqualToString: @"PLAY"]) {
        self.typeId = 12;
    } else if ([_type  isEqualToString: @"BUY"]) {
        self.typeId = 11;
    } else if ([_type  isEqualToString: @"SPORT"]) {
        self.typeId = 10;
    }
}

#pragma mark - PullTableViewDelegate
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView
{
    NSLog(@"refresh...");
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    self.currentIndex = -1;
    [self getStoreList];
}

-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    NSLog(@"loadMore...");
    [self getStoreList];
}

- (void)resetPullTableViewState
{
    [self setPullTableIsRefreshing:NO];
    [self setPullTableIsLoadMore:NO];
}


- (void)setEnableRefresh:(BOOL)flag
{
    ((PullTableView*)self.tableView).enableRefresh = flag;
}

- (void)setEnableLoadMore:(BOOL)flag
{
    ((PullTableView*)self.tableView).enableLoadingMore = flag;
}

- (void)setPullRefreshDate:(NSDate *)date
{
    ((PullTableView*)self.tableView).pullLastRefreshDate = date;
}

- (void)setPullTableIsRefreshing:(BOOL)refreshing
{
    ((PullTableView*)self.tableView).pullTableIsRefreshing = refreshing;
}
- (void)setPullTableIsLoadMore:(BOOL)loadMore
{
    ((PullTableView*)self.tableView).pullTableIsLoadingMore = loadMore;
}

- (void)reloadRowIndexInSection:(int)section row:(int)row
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}


@end
